package iosr11.Service;

import iosr11.contact.dao.ContactsHelper;
import iosr11.data.Attendence;
import iosr11.data.Event;
import iosr11.data.EventSeries;
import iosr11.data.ProjectGroup;
import iosr11.data.Student;
import iosr11.data.StudentEventSeries;
import iosr11.gbase.dao.AttendenceDAO;
import iosr11.gbase.dao.EventDAO;
import iosr11.gbase.dao.EventSeriesDAO;
import iosr11.gbase.dao.StudentEventSeriesDAO;
import iosr11.gbase.dao.TeacherDAO;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

public class EventServiceImpl implements EventService {

	private EventDAO eventDAO;
	private EventSeriesDAO eventSeriesDAO;
	private StudentEventSeriesDAO studentEventSeriesDAO;
	private AttendenceDAO attendenceDAO;
	private ContactsHelper contactsDAO;
	private TeacherDAO teacherDAO;
	private Logger log = Logger.getLogger("service-log");
	
	
	
	@Override
	public Class<Event> getServiceEntityClass() {
		return Event.class;
	}


	@Override
	public boolean createEvent(Date startDate, Date endDate, String place,
			EventSeries series, boolean deadline, String description) {
		Event event = new Event();
		event.setStartDate(startDate);
		event.setEndDate(endDate);
		event.setPlace(place);
		event.setSeries(series);
		event.setDeadline(deadline);
		event.setDescription(description);
		return this.addEvent(event);
	}


	@Override
	public boolean addEvent(Event event) {
		return eventDAO.add(event);
	}
	
	

	@Override
	public boolean addEventToEventSeries(Event event, EventSeries series) {
		if (series==null){
			log.warning("brak podanej serii eventu");
			return false;
		}
		if (event==null){
			log.warning("brak podanego eventu");
			return false;
		}
		log.info("dodawanie serii eventow do eventu");
		event.setSeries(series);
		series.addEvent(event);
		return eventDAO.update(event);
	}


	@Override
	public boolean removeEventFromEventSeries(Event event, EventSeries series) {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public Event getEvent(String id) {
		Event event = eventDAO.getByID(id);
		
		// notatki jeszcze nie uzywane
		event.setNotes(null);
		if (event.getSeries()!=null)
			if (event.getSeries().getId()!=null){
				EventSeries series = eventSeriesDAO.getByID(event.getSeries().getId());
				event.setSeries(series);
				log.info("pobrano informacje o serii dla eventu");
			}
		// jeszcze nie wykorzystywane
/*		List<Attendence> attendences = attendenceDAO.getEventAttendence(event);
		if (attendences==null){
			log.info("brak obecnosci - null");
		}
		else if (attendences.size()<1){
			log.info("brak obecnosci (size<1)");
		}
		event.setAttendences(attendences);*/
		return event;
	}
	
	

	@Override
	public List<Event> getEventsForSeries(EventSeries series) {
		if (series==null) return null;
		return eventDAO.getEventForSeries(series);
		//return null;
	}


	@Override
	public boolean deleteEvent(Event event) {
		log.info("usuwanie eventu");
		return eventDAO.delete(event);
	}

	@Override
	public boolean updateEvent(Event event) {
		log.info("aktualizacja danych eventu");
		return eventDAO.update(event);
	}

	@Override
	public List<ProjectGroup> getGroupsForCurrentEvent() {
		Event event = this.getCurrentEvent();
		if (event==null){
			log.info("brak obecnie eventu");
			return new ArrayList<ProjectGroup>();
		}
		if (event.getSeries()==null){
			log.info("brak serii eventow -blad");
			return new ArrayList<ProjectGroup>();
		}
		ProjectGroupService groupService = (ProjectGroupService) ServiceLocatorImpl.getInstance().getService(ProjectGroup.class);
		return groupService.getEventGroup(event);
	}


	@Override
	public List<Student> getStudentsForCurrentEvent() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<Event> getStudentEvents(Student student) {
		if (student==null){
			log.warning("nie podano studenta");
			return new ArrayList<Event>();
		}
		List <EventSeries> series = new ArrayList<EventSeries>();
		List<StudentEventSeries> tmp0 = studentEventSeriesDAO.getEventSeriesForStudent(student);
		if (tmp0!=null){
			for (StudentEventSeries se : tmp0){
				EventSeries series_tmp = eventSeriesDAO.getByID(se.getSeries().getId());
				// dodanie danyc o nauczycielu
				if (series_tmp.getTeacher()!=null)
					if (series_tmp.getTeacher().getId()!=null){
						series_tmp.setTeacher(teacherDAO.getByID(series_tmp.getTeacher().getId()));
						if (series_tmp.getTeacher().getUser()!=null){
							series_tmp.getTeacher().setUser(contactsDAO.getUserByRef(series_tmp.getTeacher().getUser().getId()));
							log.info("wypelnianie kontaktu dla nauczyciela");
						}
					}
				series.add(series_tmp);
			}
		}
		else { 
			return new ArrayList<Event>();
		}
		
		
		if (series == null) return new ArrayList<Event>();
		List<Event> result = new ArrayList<Event>();
		for (EventSeries tmp : series){
			List<Event> events = eventDAO.getEventForSeries(tmp);
			if (events != null){
				for (Event event : events){
					event.setSeries(tmp);
				}
				result.addAll(events);
			}
		}
		SortResult.sortEvents(result);
		return result;
	}
	

	
	
	@Override
	public Event getCurrentEvent() {
		List<Event> events = this.getAllEvents();
		Calendar cal = Calendar.getInstance();
		Date date = cal.getTime();
		if (events==null){
			log.info("brak eventow");
			return null;
		}
		//List<Event> result = new ArrayList<Event>();
		for(Event tmp : events){
			if (tmp!=null){
				if(tmp.getStartDate().getTime()<=date.getTime()){
					if (tmp.getEndDate().getTime()>=date.getTime()){
						log.info("znaleziono event dostepny obecnie");
						//result.add(tmp);
						Event event = this.getEvent(tmp.getId());
						AttendenceService attService = (AttendenceService) ServiceLocatorImpl.getInstance().getService(Attendence.class);
						List<Attendence> attendence = attService.getAttendencesForEvent(event);
						
						//event.setAttendences(attendence);
						return event;
					}
				}
			}
		}
		return null;
	}


	@Override
	public List<Event> getAllEvents() {
		log.info("wyswietlenie wszystkich eventow zapisanych w bazie");
		List<Event> events = eventDAO.getAll();
		for(Event ev : events){
			if (ev.getSeries()!=null) {
				ev.setSeries(eventSeriesDAO.getByID(ev.getSeries().getId()));
				if (ev.getSeries().getTeacher()!=null){
					ev.getSeries().setTeacher(teacherDAO.getByID(ev.getSeries().getTeacher().getId()));
					if (ev.getSeries().getTeacher().getUser()!=null){
						ev.getSeries().getTeacher().setUser(contactsDAO.getUserByRef(ev.getSeries().getTeacher().getUser().getId()));
					}
				}
			}
		}
		SortResult.sortEvents(events);
		return events;
	}

	
	// ==============================================================
	// settery dla springa

	public void setEventDAO(EventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}

	public void setEventSeriesDAO(EventSeriesDAO eventSeriesDAO) {
		this.eventSeriesDAO = eventSeriesDAO;
	}

	public void setStudentEventSeriesDAO(StudentEventSeriesDAO studentEventSeriesDAO) {
		this.studentEventSeriesDAO = studentEventSeriesDAO;
	}

	public void setTeacherDAO(TeacherDAO teacherDAO) {
		this.teacherDAO = teacherDAO;
	}

	public void setAttendenceDAO(AttendenceDAO attendenceDAO) {
		this.attendenceDAO = attendenceDAO;
	}

	public void setContactsDAO(ContactsHelper contactsDAO) {
		this.contactsDAO = contactsDAO;
	}
	
	

}
